/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.server.file;

import swym.server.*;
import swym.server.config.*;

import java.util.*;
import java.io.*;

/**
 *
 * @author mschaefer
 */
public class FileActionManager 
{   
    private Vector<FileAction> fileActions;
    
    public FileActionManager()
    {
        fileActions = new Vector<FileAction>();
    }
    
    /**
     * Adds a client to each file action's client update table
     * @param client
     */
    public void addClient( Client client )
    {
        synchronized ( fileActions )
        {
            for ( FileAction fileAction : fileActions )
                fileAction.addClient( client );
        }
    }
    
    /**
     * Add a new file action to the manager for this user.
     * 
     * These changes will be broadcasted to any actively-
     * connected clients, and will be queued up by
     * the manager.
     * @param fileAction
     */
    public void addFileAction( FileAction fileAction )
    {
        synchronized ( fileActions )
        {
            fileActions.add( fileAction );
            
            // Tell all of the clients (if they have connections)
            // that there's a new file action waiting for them
            for ( Client client : fileAction.getClients() )
            {
                if ( !fileAction.isClientUpdated( client ) )
                    client.alertNewFileAction( fileAction );
            }
                
        }
    }
    
    public FileAction getFileAction( String filePath, String version )
    {
        FileAction action = null;
        
        synchronized ( fileActions )
        {
            for ( FileAction fileAction : fileActions )
            {
                if ( fileAction.getFile().getPath().equals( filePath ) &&
                     fileAction.getFile().getVersion().equals( version ) )
                {
                    action = fileAction;
                    break;
                }
            }
        }
        
        return action;
    }

    public Vector<FileAction> getFileActions( String filePath )
    {
        Vector<FileAction> actions = new Vector<FileAction>();
    
        synchronized ( fileActions )
        {
            for ( FileAction fileAction : fileActions )
            {
                if ( fileAction.getFile().getPath().equals( filePath ) )
                    actions.add( fileAction );
            }
        }
        
        return actions;
    }
    
    public Vector<FileAction> getFileActions( FileAction.eFileActionType type )
    {
        Vector<FileAction> actions = new Vector<FileAction>();
    
        synchronized ( fileActions )
        {
            for ( FileAction fileAction : fileActions )
            {
                if ( fileAction.getType().equals( type ) )
                    actions.add( fileAction );
            }
        }
        
        return actions;
    }
    
    public Vector<FileAction> getFileActions( )
    {
        synchronized ( fileActions )
        {
            return new Vector<FileAction>( fileActions );
        }
    }
    
    /**
    public void writeAllToDisk()
    {
        Vector<SyncedFile> fileValues = new Vector<SyncedFile>( files.values() );
        
        for ( SyncedFile file : fileValues )
        {
            try
            {
                file.writeToDisk();
            }
            catch ( IOException e )
            {
                // What to do if we can't write a file!?
            }
        }
    }
     * **/
    
    /**
     * Returns the path to the place where the user's file system
     * will be stored on the disk
     * @param owner
     * @return
     */
    public static String getUserRootPath( User owner )
    {
        return Settings.getBasePath() + "users" + File.separator + 
                                        owner.getName() + File.separator;
    }
}
